#version 460

#include "project_matrix_basic.glsl"
#include "single_color.glsl"


#define M_PI 3.1415926535897932384626433832795







layout(points) in;
layout(line_strip, max_vertices=96) out;
//layout(lines, max_vertices=128) out;

in gl_PerVertex {
	vec4	gl_Position;
	float	gl_PointSize;
} gl_in[];

//layout(location = 0) in vec4 gColor[];

layout(location = 1) in VS_GS_VERTEX {
	uint InstanceIndex;
}vertex_in[];

 


layout(location = 0) out vec4 gColor;
out gl_PerVertex {
	vec4	gl_Position;
	float	gl_PointSize;
};



/*struct S_Materix {
	vec2 begin_coord;
	vec2 control_coord;
	vec2 end_coord;
	vec2 radius_z;
};*/



// 位置  大小 缩放和深度
layout(std430, column_major, binding = 2) readonly buffer U1_Matrix {
	mat3x2 m_LP_SD[];
};



//vec2 pos[4] = {vec2(10.0, 10.0), vec2(100.0, 10.0), vec2(10.0, 100.0), vec2(100.0, 100.0)};

void main(void){
	mat3x2 mat = m_LP_SD[vertex_in[0].InstanceIndex];
	
	vec4 TColor = f_buildColor(InstanceColor[vertex_in[0].InstanceIndex]);



	vec4 v = vec4(0.0, 0.0, 0.0, 1.0);
	float a = radians(6);


	vec2 size = mat[1] - mat[0];
	float PN = sign(size.x);



	float PN_Reverse = 1;

	float x = abs(dot(vec2(1.0, 0.0), normalize(size)));
	int num_public = int(15 * x);

	//radius_public = x*mat[2].x;
	//以Y轴方向判断半径大小
	float radius_public = min(1, abs(size.y / (mat[2].x*2))) * mat[2].x;


	

	float dir = min(1, length(size) / (mat[2].x*4));
	float radius = mat[2].x * dir;
		
	int num = num_public;
	int num2 = num_public;
	float y = 0;
	
	if(size.y >= 0){
		//以X轴方向判断半径大小
		radius_public = min(x*mat[2].x, radius_public);
	}
	radius = max(radius_public, mat[2].x * 0.4);
	radius = min(radius_public, radius);
	







	if(size.y < 0) {
		if(abs(size.x) >= radius*2){
			
			size = abs(size);
			size.x -= radius * 4;
			size.y += radius;
			
			y = max(0, dot(vec2(0.0, 1.0), normalize(size)));
			if(size.x >= 0){
				num = 15 + int(15 * y);
				num2 = 15 + int(15 * (y));
			}else{
				num = 30 + int(19.0 * (1.0-y));
				num2 = 30 + int(19.0 * (1.0-y));
			}

		}else{
			PN_Reverse = -PN_Reverse;

			size = abs(size);
			y = dot(vec2(1.0, 0.0), normalize(size));

			num = 30 - int(15 * (y));
			num2 = 30 + int(15 * (y));
			
		}
		
	}




	//=============== 判断开始点是输入还是输出插座 =================
	if (mat[2].y != 0) {
		num = max(1, num);
		
		for(int i=0; i<num; ++i){
			v.x = (-cos(a*i) * radius + radius) * PN;
			v.y = sin(a*i) * radius;
			v.z = mat[2].y;
			

			gl_Position = v;
			gl_Position.xy += mat[0];
			gl_Position.xy *= m_Scale;
			gl_Position.xy += m_Loc;
			gl_Position = mat_ProjecView * gl_Position;
			gColor = TColor;
			EmitVertex();
		}


		gColor = TColor;
		gl_Position = vec4(mat[1], 0, 1);
		gl_Position.xy *= m_Scale;
		gl_Position.xy += m_Loc;
		gl_Position = mat_ProjecView * gl_Position;
		EmitVertex();

		EndPrimitive();
		return;

	}else{

		gColor = TColor;
		gl_Position = vec4(mat[0], 0, 1);
		gl_Position.xy *= m_Scale;
		gl_Position.xy += m_Loc;
		gl_Position = mat_ProjecView * gl_Position;
		EmitVertex();

		

		int offset = 30 - num2;
		num2 = max(1, num2);
		
		for(int i=0; i<num2; ++i){
			v.x = (-cos(a*(i+offset)) * radius - radius) * PN * PN_Reverse;
			v.y = -sin(a*(i+offset)) * radius;
			v.z = mat[2].y;

			gl_Position = v;
			gl_Position.xy += mat[1];
			gl_Position.xy *= m_Scale;
			gl_Position.xy += m_Loc;
			gl_Position = mat_ProjecView * gl_Position;
			gColor = TColor;
			EmitVertex();
		}


		EndPrimitive();
		return;

	}
	
	

	gColor = TColor;
	gl_Position = vec4(mat[1], 0, 1);
	gl_Position.xy *= m_Scale;
	gl_Position.xy += m_Loc;
	gl_Position = mat_ProjecView * gl_Position;
	EmitVertex();

	EndPrimitive();
	return;
	
	
}



 